<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- datastorage.qdoc -->
  <title>Data Storage | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Data Storage</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#saving-and-loading-data">Saving and Loading Data</a></li>
<li class="level1"><a href="#sql-support-in-qt">SQL Support in Qt</a></li>
<li class="level1"><a href="#xml-support-in-qt">XML Support in Qt</a></li>
<li class="level1"><a href="#json-in-qt">JSON in Qt</a></li>
<li class="level1"><a href="#local-storage">Local Storage</a></li>
<li class="level1"><a href="#qsettings-class">QSettings Class</a></li>
<li class="level1"><a href="#resources">Resources</a></li>
<li class="level1"><a href="#file-archiving">File Archiving</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Data Storage</h1>
<span class="subtitle"></span>
<!-- $$$topics-data-storage.html-description -->
<div class="descr"> <a name="details"></a>
<a name="saving-and-loading-data"></a>
<h2 id="saving-and-loading-data">Saving and Loading Data</h2>
<p>The <a href="../qtcore/qiodevice.html">QIODevice</a> class is the base class for all file and data storage devices in <a href="../qtcore/qtcore-index.html">Qt Core</a>. All classes that are used for reading and writing data inherit from it.</p>
<p>Examples of devices are <a href="../qtcore/qfile.html">QFile</a>, <a href="../qtcore/qbuffer.html">QBuffer</a>, <a href="../qtnetwork/qtcpsocket.html">QTcpSocket</a>, and <a href="../qtcore/qprocess.html">QProcess</a>. <a href="../qtcore/qfile.html">QFile</a> is used for reading and writing text, binary files, and resources. The <a href="../qtcore/qbuffer.html">QBuffer</a> class provides a <a href="../qtcore/qiodevice.html">QIODevice</a> interface for a <a href="../qtcore/qbytearray.html">QByteArray</a>. <a href="../qtnetwork/qtcpsocket.html">QTcpSocket</a> enables the developer to establish a TCP connection and transfer streams of data. <a href="../qtcore/qprocess.html">QProcess</a> is used to start external programs, and to read from and write to that process.</p>
<ul>
<li><a href="../qtcore/io.html">Input/Output and Networking</a> (list of I/O related classes)</li>
<li><a href="../qtcore/io-functions.html">File and Datastream Functions</a></li>
<li><a href="../qtcore/datastreamformat.html">Serializing Qt Data Types</a></li>
</ul>
<a name="sql-support-in-qt"></a>
<h2 id="sql-support-in-qt">SQL Support in Qt</h2>
<p>The <a href="../qtsql/qtsql-index.html">Qt SQL</a> module uses driver plugins to communicate with several database APIs. Qt has drivers for <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a>, MySQL, DB2, Borland InterBase, Oracle, ODBC, and PostgreSQL. It is also possible to develop your own driver if Qt does not provide the driver needed.</p>
<p>Qt's SQL classes can be divided in 3 layers:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Layer</th><th >Purpose</th><th >Example class</th></tr></thead>
<tr valign="top" class="odd"><td ><ul>
<li>Driver layer</li>
<li>SQL API layer</li>
<li>User Interface layer</li>
</ul>
</td><td ><ul>
<li>Low-level communication between database and the SQL API layer</li>
<li>Provide access to databases</li>
<li>Link data from a database to data-aware widgets</li>
</ul>
</td><td ><ul>
<li><a href="../qtsql/qsqldriver.html">QSqlDriver</a>, <a href="../qtsql/qsqldrivercreator.html">QSqlDriverCreator</a></li>
<li><a href="../qtsql/qsqldatabase.html">QSqlDatabase</a>, <a href="../qtsql/qsqlquery.html">QSqlQuery</a></li>
<li><a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqlquerymodel">QSqlQueryModel</a> (readonly), <a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqltablemodel">QSqlTableModel</a> (read/write), <a href="../qtsql/qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> (read/write with foreign-key support)</li>
</ul>
</td></tr>
</table></div>
<p>With the MySQL driver, it is possible to connect to a MySQL server. In order to build the QMYSQL Plugin for Unix or macOS, you need the MySQL header files as well as the shared library, libmysqlclient.so. To compile the plugin for Windows, install MySQL.</p>
<p>If you use the embedded MySQL Server, you do not need a MySQL server in order to use that database system. In order to do so, you need to link the Qt plugin to libmysqld instead of libmysqlclient.</p>
<p>The Qt <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> plugin is very suitable for local storage. <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> is a relational database management system contained in a small (~350 KiB) C library. In contrast to other database management systems, <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> is not a separate process that is accessed from the client application, but an integral part of it. <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> operates on a single file, which must be set as the database name when opening a connection. If the file does not exist, <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> will try to create it.</p>
<p><a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> has some restrictions regarding multiple users and multiple transactions. If you are reading or writing on a file from different transactions, your application might freeze until one transaction commits or rolls back.</p>
<p>Once the driver part is set up, the data is accessible using the classes, <a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqlquerymodel">QSqlQueryModel</a>, <a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqltablemodel">QSqlTableModel</a>, and <a href="../qtsql/qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>. <a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqltablemodel">QSqlTableModel</a> and <a href="../qtsql/qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> provide editable models that can used with Qt's item views. <a href="../qtquickcontrols/qtquickcontrols-chattutorial-example.html#qsqltablemodel">QSqlTableModel</a> has read/write access to a single table, whereas <a href="../qtsql/qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> has read/write access to the main table (not to the table with the foreign key).</p>
<p>The following pages contain information about incorporating SQL into applications:</p>
<ul>
<li><a href="../qtsql/sql-programming.html">SQL Programming</a><ul>
<li><a href="../qtsql/sql-programming.html#database-classes">Database Classes</a></li>
<li><a href="../qtsql/sql-sqlstatements.html">Executing SQL Statements</a></li>
<li><a href="../qtsql/sql-model.html">Using the SQL Model Classes</a></li>
<li><a href="../qtsql/sql-presenting.html">Presenting Data in a Table View</a></li>
<li><a href="../qtsql/sql-forms.html">Creating Data-Aware Forms</a></li>
</ul>
</li>
</ul>
<a name="xml-support-in-qt"></a>
<h2 id="xml-support-in-qt">XML Support in Qt</h2>
<p>Qt provides APIs to read and parse XML streams, and also to write to these streams. The following key classes facilitate these actions by providing the necessary infrastructure:</p>
<ul>
<li><a href="../qtcore/qxmlstreamreader.html">QXmlStreamReader</a> class provides a parser to read XML. It is a well-formed XML 1.0 parser that does not include external parsed entities.<ul>
<li>It understands and resolves XML namespaces. For example, in case of a StartElement, namespaceUri() returns the namespace the element is in, and <a href="../qdoc/13-qdoc-commands-topics.html#name">name</a>() returns the element's local name. The combination of namespaceUri() and <a href="../qdoc/13-qdoc-commands-topics.html#name">name</a>() uniquely identifies an element.</li>
<li>It is not CPU-intensive, as it doesn't store the entire XML document tree in memory. It only stores the current token at the time it is reported.</li>
</ul>
</li>
<li>The <a href="../qtcore/qxmlstreamwriter.html">QXmlStreamWriter</a> class provides an XML writer with a simple streaming API. It is the counterpart to <a href="../qtcore/qxmlstreamreader.html">QXmlStreamReader</a> for writing XML, and it operates on a <a href="../qtcore/qiodevice.html">QIODevice</a> specified with setDevice().<ul>
<li>It is a simple API that provides a dedicated function for every XML token or event you want to write.</li>
<li>It takes care of prefixing namespaces based on the namespaceUri specified while writing elements or attributes. If you have to use certain standardized prefixes, you can force the writer to use them by declaring the namespaces manually with either writeNamespace() or writeDefaultNamespace().</li>
<li>It can automatically format the generated XML data by adding line-breaks and indentation, making it readable. This feature can be turned on with the auto-formatting property.</li>
<li>It encodes XML in UTF-8 by default. Different encodings can be enforced using setCodec().</li>
</ul>
</li>
</ul>
<p>Besides reading and writing to XML streams, Qt also provides APIs for the following additional use cases:</p>
<ul>
<li>Querying an XML data source using <a href="../qtxmlpatterns/xmlprocessing.html">XQuery</a> and XPath</li>
<li>XML transformation using XSLT</li>
<li>XML schema validation</li>
</ul>
<p>The following topics provide more insight into Qt XML support:</p>
<ul>
<li><a href="../qtxml/xml-namespaces.html">An Introduction to Namespaces</a></li>
<li><a href="../qtxml/xml-streaming.html">XML Streaming</a></li>
<li><a href="../qtxmlpatterns/xquery-introduction.html">A Short Path to XQuery</a></li>
<li><a href="../qtxmlpatterns/qml-qtquick-xmllistmodel-xmllistmodel.html">XmlListModel</a></li>
<li><a href="../qtxmlpatterns/qtxmlpatterns-index.html">Qt XML Patterns</a></li>
</ul>
<a name="json-in-qt"></a>
<h2 id="json-in-qt">JSON in Qt</h2>
<p>JSON is a text-based open standard for data interchange that is easy to read and parse. It is used for representing simple data structures and associative arrays, called objects. It is related to JavaScript, but is a language-independent notation form.</p>
<p>An object can take 2 forms:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><ul>
<li>Collection of name/value pairs</li>
<li>Ordered list of values</li>
</ul>
</tr></thead>
<tr valign="top" class="odd"><td ><ul>
<li>{ &quot;last_name&quot;: &quot;Routledge&quot;, &quot;first_name&quot;: &quot;Ronald&quot;, &quot;birth_date&quot;: 1960 }</li>
<li>&quot;colours&quot;: [&quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;]</li>
</ul>
</td><td ></td></tr>
</table></div>
<ul>
<li><a href="../qtcore/json.html">JSON Support in Qt</a></li>
<li><a href="../qtcore/qtcore-serialization-savegame-example.html">JSON Save Game Example</a></li>
</ul>
<a name="local-storage"></a>
<h2 id="local-storage">Local Storage</h2>
<p>The Local Storage API provides the ability to access local offline storage in an SQL database from QML and JavaScript.</p>
<p>These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QDeclarativeEngine::offlineStoragePath() as <a href="../qtsql/qtsql-attribution-sqlite.html#sqlite">SQLite</a> databases (<a href="../qtsql/sql-driver.html">SQL Database Drivers</a>).</p>
<p>The API conforms to the Synchronous API of the HTML5 Web Database API, W3C Working Draft 29 October 2009 (<a href="http://www.w3.org/TR/2009/WD-webdatabase-20091029/">HTML5 Web Database API</a>).</p>
<p>See <a href="../qtquick/qtquick-localstorage-example.html">Qt Quick Examples - Local Storage</a> for a basic demonstration of using the Local Storage API.</p>
<ul>
<li><a href="../qtquick/qtquick-localstorage-qmlmodule.html">Qt Quick Local Storage QML Types</a></li>
</ul>
<a name="qsettings-class"></a>
<h2 id="qsettings-class">QSettings Class</h2>
<p>The <a href="../qtcore/qsettings.html">QSettings</a> class provides persistent storage of application settings. An application usually remembers its settings from the previous session.</p>
<p>Settings are stored differently on different platforms. For example, on Windows they are stored in the registry, whereas on macOS they are stored in XML files.</p>
<p><a href="../qtcore/qsettings.html">QSettings</a> enable you to save and restore application settings in a portable manner. Constructing and destroying a <a href="../qtcore/qsettings.html">QSettings</a> object is lightweight and fast. While creating an object of <a href="../qtcore/qsettings.html">QSettings</a>, it is a good practice to specify not only the name of the application, but also the name of your organization. For example:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span> settings(<span class="string">&quot;MyCompany&quot;</span><span class="operator">,</span> <span class="string">&quot;Accountancy&quot;</span>);

</pre>
<a name="resources"></a>
<h2 id="resources">Resources</h2>
<p>The Qt Resource System is a platform-independent mechanism for storing binary files in the application's executable. This is handy if your application frequently needs a certain file, or set of files. It also protects against loss of that particular file .</p>
<p>Resource data can either be compiled into the binary and accessed immediately in the application code, or a binary resource can be created dynamically and registered with the resource system by the application.</p>
<p>By default, resources are accessible from the application code by the same file name as they are stored in the source tree, with a <code>:/</code> prefix, or by a URL with a qrc scheme.</p>
<ul>
<li><a href="../qtcore/resources.html">The Qt Resource System</a></li>
</ul>
<a name="file-archiving"></a>
<h2 id="file-archiving">File Archiving</h2>
<p>An archive file is a collection of files or directories which are generally compressed in order to reduce the space they would otherwise consume on a drive. Examples of archive files are ZIP, TAR, RAR and 7z.</p>
<p>Qt has support for archives produced by zlib (see <a href="http://qt-project.org/faq/answer/how_to_compress_data_with_qt">qCompress() and qUncompress()</a>).</p>
</div>
<!-- @@@topics-data-storage.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
